package so.wy.servicecenter.businessbed.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;
import so.wy.servicecenter.base.exception.WebException;
import so.wy.servicecenter.base.frame.api.httpexception.HttpExceptionHandler;
import so.wy.servicecenter.base.frame.baseopt.BaseOptHttp;
import so.wy.servicecenter.base.frame.baseopt.BaseOptWeb;
import so.wy.servicecenter.businessbed.service.device.DeviceDTO;
import so.wy.servicecenter.businessbed.web.DeviceWeb;


/**
 * @author wdl
 * @date 2020-04-08
 */
@RestController
@Scope("request")
@RequestMapping("/device")
public class DeviceHttp extends BaseOptHttp<DeviceDTO> {
    @Autowired
    private DeviceWeb deviceWeb;

    @Override
    protected BaseOptWeb getOptWeb() {
        return deviceWeb;
    }

    @HttpExceptionHandler
    @PutMapping("/edit/run/info/{serial}")
    public String updateDeviceRunInfo(@PathVariable("serial")String serial, Double batteryBoard, Double batteryLock, String hardVer, String softVer) {
        deviceWeb.updateDeviceRunInfo(serial, batteryBoard, batteryLock, hardVer, softVer);
        return toHttpJson();
    }

    @HttpExceptionHandler
    @GetMapping("/details/serial")
    public String getDeviceInfoBySerial(String serial) throws WebException {
        return toHttpJson(deviceWeb.getDeviceInfoBySerial(serial));
    }

    @HttpExceptionHandler
    @PostMapping("/publish/on")
    public String publishDevice(Long deviceId, Long floorId, Long departId) throws WebException {
        deviceWeb.publishDevice(deviceId, floorId, departId);
        return toHttpJson();
    }

    @HttpExceptionHandler
    @PostMapping("/publish/down")
    public String publishDownDevice(Long deviceId) throws WebException {
        deviceWeb.publishDownDevice(deviceId);

        return toHttpJson();
    }
}